package com.neu.gulimall.search.controller;

import com.neu.common.exception.BizCodeEnum;
import com.neu.common.to.EsSkuModel;
import com.neu.common.utils.R;
import com.neu.gulimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSearchSaveController {

    @Autowired
    private ProductSaveService productSaveService;

    /**
     * 上架商品
     * @param models
     * @return
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<EsSkuModel> models){
        Boolean status = false;
        try {
            status = productSaveService.productStatusUp(models);
        } catch (IOException e) {
            log.error("ElasticSearchSaveController=商品上架错误：{}",e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }

        if(status){
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }else {
            return R.ok();
        }
    }

}
